from django.db import models


class Department(models.Model):
    """ 部门表 """
    title = models.CharField(verbose_name='标题', max_length=32)

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """ 用户表 """
    sid = models.CharField(verbose_name="学号", max_length=16)
    name = models.CharField(verbose_name="姓名", max_length=16)
    password = models.CharField(verbose_name="密码", max_length=64)
    age = models.IntegerField(verbose_name="年龄")
    # 无约束
    # depart_id = models.BigIntegerField(verbose_name="部门ID")
    # 1.有约束
    #   - to，与那张表关联
    #   - to_field，表中的那一列关联
    # 2.django自动
    #   - 写的depart
    #   - 生成数据列 depart_id
    # 3.部门表被删除
    # ### 3.1 级联删除
    depart = models.ForeignKey(verbose_name="部门", to="Department", to_field="id", on_delete=models.CASCADE)
    # ### 3.2 置空
    # depart = models.ForeignKey(to="Department", to_field="id", null=True, blank=True, on_delete=models.SET_NULL)

    # 在django中做的约束
    gender_choices = (
        (1, "男"),
        (2, "女"),
    )
    gender = models.SmallIntegerField(verbose_name="性别", choices=gender_choices)

    def __str__(self):
        return self.name


class Admin(models.Model):
    """ 管理员 """
    username = models.CharField(verbose_name="用户名", max_length=32)
    password = models.CharField(verbose_name="密码", max_length=64)

    level_choices = (
        (1, "1级"),
        (2, "2级"),
        (3, "3级"),
    )
    level = models.SmallIntegerField(verbose_name="级别", choices=level_choices, default=3)

    def __str__(self):
        return self.username


class Record(models.Model):
    """签到记录"""
    signin_time = models.DateTimeField(verbose_name="签到时间")
    user_info = models.ForeignKey(verbose_name="用户", to="UserInfo", to_field="id", on_delete=models.CASCADE)
    ip = models.CharField(verbose_name="IP地址", max_length=16)
    is_face_choices = (
        (0, "是"),
        (1, "否"),
    )
    is_face = models.SmallIntegerField(verbose_name="是否人脸", choices=is_face_choices, default=1)

# manage.py makemigrations
# manage.py migrate
